# @Author   : tan90
# @Time     : 2025/3/18 10:32
from tkinter import *
from tkinter import messagebox


def hit_me():
    print("hello, world")
    messagebox.showinfo("提示框", "why hit me?")


class Application(Frame):
    def __init__(self, master=None):
        # 方式一
        super().__init__(master)
        # 方式二
        # Frame.__init__(self, master)
        self.btn1 = None
        self.btn2 = None
        self.btn3 = None
        self.btn11 = None
        self.btn12 = None
        self.btn13 = None
        self.lab1 = None
        self.lab2 = None
        self.entry1 = None
        self.entry2 = None
        self.text1 = None
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.btn1 = Button(self, text='hit me', command=hit_me)
        self.btn1.pack()

        self.btn2 = Button(self, text='退出', command=self.master.destroy, state=DISABLED)
        self.btn2.pack()

        self.lab1 = Label(self, text='标签1')
        self.lab1.pack()

        self.lab2 = Label(self, text='我\n是\n男人', justify=LEFT, borderwidth=2, relief=GROOVE)
        self.lab2.pack()

        # 输入框测试
        self.entry1 = Entry(self, textvariable=StringVar(self, ""))
        self.entry1.pack()
        self.entry2 = Entry(self, textvariable=StringVar(self), show='*')
        self.entry2.pack()
        self.btn3 = Button(self, text='登录', command=self.login)
        self.btn3.pack()

        # Text
        self.text1 = Text(self, width=30, height=5)
        self.text1.pack()

        #Button
        self.btn11 = Button(self, text='插入', command=self.insertText)
        self.btn11.pack(side=LEFT)

        self.btn12 = Button(self, text='获取')
        self.btn12.bind("<Button-1>", self.getText)
        self.btn12.pack(side=LEFT)

        self.btn13 = Button(self, text='清空', command=lambda: self.cleanText('hello'))
        self.btn13.pack(side=LEFT)

        self.btn13.bind("<Button-1>", self.cleanText)

        # Radiobutton

    def login(self):
        user = self.entry1.get()
        password = self.entry2.get()
        print(user, password)
        messagebox.showinfo("提示框", "欢迎您：" + user)

    def insertText(self):
        self.text1.insert(1.0, "hello world")

    def getText(self,event):
        print(event.widget)
        print(self.text1.get(1.0, END))

    def cleanText(self, event):
        print(event)
        self.text1.delete(1.0, END)


if __name__ == '__main__':
    app = Tk()
    app.geometry("400x300+100+100")
    app.title("我的第一个GUI程序")
    Application(master=app)
    app.mainloop()
